/**
 * 用户相关状态数据
 * Created by yuzhou on 16/9/14.
 */

define([
    'vuex/mutation-types'
], function(types){

    var state = {
        me: null, //当前登录用户
        flatTree: null,
        tree: {
            id: '0',
            name: '系统导航菜单',
            children: []
        },
        result: null, // 上次添加/更新用户的结果
        currentUser: null, // 当前正在操作的用户原始数据
        currentUserIndex: -1,
        updated: {}, // 用户本地更新的记录
        page: {
            list: [], // 已经获取用户数据列表
            lastTimestamp: -1, //上次筛选数据的分割时间
            updated: 0, // 上次筛选数据后新更新用户数
            pageNumber: 0, // 当页已经加载的页数
            pageSize: 50, // 每页数据容量
            keywords: '', // 查询关键字
            totalPage: 0, // 总页数
            totalRow: 0, // 总条数,
            firstPage: false,
            lastPage: false
        }
    }

    function updateGroupNode(tree, id, active) {
        if(tree.children && tree.children.length > 0) {
            var found = false // 对应节点是否在当前children列表中
            for(var i=0; i < tree.children.length; i++) {
                if(tree.children[i].id == id) {
                    found = true
                    tree.children[i].active = active
                    break
                }
            }

            if(found) { // 找到
                if(active) {
                    for(var i=0; i < tree.children.length; i++) {
                        if(tree.children[i].id != id && tree.children[i].children) {
                            tree.children[i].active = false
                        }
                    }
                }
            } else { //没找到，递归到下一级节点继续查找
                for(var i=0; i < tree.children.length; i++) {
                    updateGroupNode(tree.children[i], id, active)
                }
            }
        }
    }

    var mutations = {}

    mutations[types.RECEIVE_LOGIN_USER] = function(state, user) {
        state.me = user
    }

    mutations[types.RECEIVE_USER_MODULE_TREE] = function(state, modules) {
        state.tree.children = modules
    }

    mutations[types.UPDATE_TREE_BY_PATH] = function(state, path) {

    }

    mutations[types.UPDATE_TREE_BY_ID] = function(state, id, active) {
        // 找到树中指定id的节点，将其active值更新，如果active是true,其同组的其它group节点需要转为false
        updateGroupNode(state.tree, id, active)
    }

    mutations[types.UPDATE_SEARCH_USER_KEYWORDS] = function(state, keywords) {
        state.page.keywords = keywords
    }

    mutations[types.RESET_CURRENT_USER] = function(state) {
        state.currentUser = null
        state.currentUserIndex = -1
    }

    mutations[types.SET_CURRENT_USER] = function(state, user, index) {
        state.currentUser = user
        state.currentUserIndex = index
    }

    mutations[types.RECEIVE_USERS] = function(state, data, timestamp) {
        console.log('Received user data:', data, timestamp)
        if(timestamp) state.page.lastTimestamp = timestamp
        if('page' in data) {
            var page = data['page']
            state.page.list = page.firstPage? page.list : state.page.list.concat(page.list)
            state.page.pageNumber = page.pageNumber
            state.page.firstPage = page.firstPage
            state.page.lastPage = page.lastPage

            if(page.firstPage) {
                state.updated = {}
                state.page.updated = 0
                state.page.totalRow = page.totalRow
                state.page.totalPage = page.totalPage
            }
        }

        if('updated' in data) {
            state.page.updated = data['updated']
        }
    }

    mutations[types.ADD_USER_REQUEST] = function(state) {
        state.result = null
    }

    mutations[types.ADD_USER_SUCCESS] = function(state, user) {
        state.result = {
            ok: true,
            title: '用户添加成功',
            msg: '用户' + user.id + '于' + new Date(user.createTime).toLocaleString() + '添加成功'
        }
        if(new RegExp(state.page.keywords).test(user.username)) {
            state.updated[user.id] = user.updateTime
            state.page.list.unshift(user)
            state.page.totalRow++
        }
    }

    mutations[types.ADD_USER_FAILURE] = function(state, error) {
        state.result = {
            ok: false,
            title: '用户添加失败',
            msg: error
        }
    }

    mutations[types.EDIT_USER_REQUEST] = function(state) {
        state.result = null
    }

    mutations[types.EDIT_USER_SUCCESS] = function(state, user) {
        state.result = {
            ok: true,
            title: '用户更新成功',
            msg: '用户' + user.id + '数据更新成功'
        }
        //state.page.list.splice(state.currentUserIndex, 1, user)
        if(state.currentUserIndex > -1) {
            state.page.list.splice(state.currentUserIndex, 1)
            state.page.list.unshift(user)
            state.currentUser = user
            state.currentUserIndex = 0
            state.updated[user.id] = user.updateTime
        }
    }

    mutations[types.EDIT_USER_FAILURE] = function(state, error) {
        state.result = {
            ok: false,
            title: '用户更新失败',
            msg: error
        }
    }

    return {
        state: state,
        mutations: mutations
    }
})


